//
//  TableMenu.m
//  Koina
//
//  Created by welldone on 13-9-7.
//  Copyright (c) 2013年 net.koina. All rights reserved.
//


#define CELL_HIGHT  35

#import <QuartzCore/QuartzCore.h>
#import "config.h"
#import "TableMenu.h"
#import "MenuListCell.h"



@interface TableMenu ()

@property (nonatomic,retain)NSString *seItemStr;
@property (nonatomic,retain)UIButton *touchhHelpBtn,*senderBtn;
@property (nonatomic,retain)NSArray *dataArray;
@property (nonatomic,retain)UITableView *listTableView;

@property(nonatomic,assign) CGRect bufRect;
@property (nonatomic) BOOL fromNbar;
-(void)touchHelpBtnOnClick;
@end

@implementation TableMenu
@synthesize dataArray,listTableView,touchhHelpBtn,myTableViewDelegate,fromNbar,senderBtn,seItemStr,bufRect;


-(id)init{
    
    self = [super init];
    if (self) {
        [self setClipsToBounds:YES];
        self.listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self addSubview:self.listTableView];
        self.listTableView.delegate = self;
        self.listTableView.dataSource = self;
        self.listTableView.backgroundView = nil;
        self.listTableView.backgroundColor = [UIColor clearColor];
        [self.listTableView setSeparatorColor:[UIColor clearColor]];
        self.listTableView.showsVerticalScrollIndicator = NO;
        
        UIImage *bgImg = [UIImage imageNamed:@"tong_03"];
        bgImg = [bgImg stretchableImageWithLeftCapWidth:bgImg.size.width/3 topCapHeight:bgImg.size.height/2];
        [self setBackgroundImage:bgImg forState:UIControlStateNormal];
        [self setBackgroundImage:bgImg forState:UIControlStateHighlighted];
    }
    return self;
}


-(void)setSelectedIte:(NSString*)strSed{
    seItemStr  = strSed;
}


-(void)setData:(NSArray*)data{
    
    if (!data||data.count<=0) {
        return;
    }
    
    NSString *maxWidth;
    for (LaunchItem  *item in data) {
        if ([maxWidth length]<[item.name length]) {
            maxWidth = item.name;
        }
    }
    
    CGSize titleSize = [maxWidth sizeWithFont:[UIFont systemFontOfSize:TextFont]];
    
    CGRect pmRect = [ UIScreen mainScreen ].applicationFrame;
    int pmH = pmRect.size.height-100;
    
    
    NSInteger maxHight = CELL_HIGHT * [data count]+20;
    if (maxHight>pmH) {
        maxHight = pmH;
    }
    
    [self setFrame:CGRectMake(0, 0, titleSize.width+50, maxHight)];
    bufRect = self.frame;
    
    [self.listTableView setFrame:CGRectMake(0, 15, self.frame.size.width, self.frame.size.height-25)];
    
    self.dataArray = data;
    
    [listTableView reloadData];
}



-(void)showInView:(UIView*)view  under:(UIButton*)senderView fromBar:(Boolean)fromBar{

    fromNbar = fromBar;
    senderBtn = senderView;
    CGRect mRect = bufRect;
    mRect.origin.x = senderView.frame.origin.x+senderView.frame.size.width/2-self.frame.size.width+30;
    
    if (fromBar) {
         mRect.origin.y = senderView.frame.origin.y+senderView.frame.size.height+14;
    }else{
        mRect.origin.y = senderView.frame.origin.y+senderView.frame.size.height+60;
    }
    
    
    [self setFrame:mRect];
       
    
     touchhHelpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [touchhHelpBtn setBackgroundColor:[UIColor clearColor]];
    [touchhHelpBtn setFrame:view.frame];
    [touchhHelpBtn addTarget:self action:@selector(touchHelpBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    [touchhHelpBtn addSubview:self];
    [view addSubview:touchhHelpBtn];
    
    
    
    const CGRect toFrame = self.frame;
    self.alpha = 0.6f;
    const CGRect tableToFrame  = self.listTableView.frame;
    self.listTableView.alpha = 0.6;
    
    CGRect sFrame = self.frame;
    sFrame.size.height = 0;
    self.frame = sFrame;
    
    CGRect tFrame = self.listTableView.frame;
    tFrame.size.height = 0;
    self.listTableView.frame = tFrame;
    
    [UIView animateWithDuration:0.3
                     animations:^(void) {
                         
                         self.alpha = 1.0f;
                         self.frame = toFrame;
                         
                         self.listTableView.frame = tableToFrame;
                         self.listTableView.alpha = 1.0f;
                         
                     } completion:^(BOOL completed) {
                         
                     }];
    

    
//    NSTimeInterval duration = 0.3;
//    CGRect rect = senderBtn.frame;
//    CGRect endRect = CGRectInset(rect, 5.0, 5.0);
//    
//    self.userInteractionEnabled = NO;
//    [self genieOutTransitionWithDuration:duration startRect:endRect startEdge:BCRectEdgeBottom completion:^{
//     self.userInteractionEnabled = YES;
//     }];
}


-(void)touchHelpBtnOnClick{
    

    CGRect toFrame = self.frame;
    toFrame.size.height = 0;
 
    [UIView animateWithDuration:0.3
                     animations:^(void) {
                         
                         self.alpha = 0.6f;
                         self.frame = toFrame;
                         
                     } completion:^(BOOL completed) {
                         [touchhHelpBtn removeFromSuperview];
                     }];
    
    
//    CGRect rect = senderBtn.frame;    
//    NSTimeInterval duration = 0.3;
//    CGRect endRect = CGRectInset(rect, 5.0, 5.0);
//
//    
//    self.userInteractionEnabled = NO;
//    [self genieInTransitionWithDuration:duration destinationRect:endRect destinationEdge:BCRectEdgeBottom completion:
//     ^{
//         self.userInteractionEnabled = YES;
//        [touchhHelpBtn removeFromSuperview];
//    }];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray count];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CELL_HIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"MenuListCell";

    MenuListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    if (cell==nil) {
        cell=[[MenuListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectedBackgroundView:nil];
//        cell.image = [UIImage imageNamed:@"icon29"];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    LaunchItem *item = [dataArray objectAtIndex:indexPath.row];
    NSString *title = item.name;


    [cell setTitle:title];
    
    if ([title isEqualToString:seItemStr]) {
        [cell setIsCur:YES];
    }else{
        [cell setIsCur:false];
    }
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    if ([myTableViewDelegate respondsToSelector:@selector(tableViewItemDidSelected:senderBtnTag:)]) {
        
         LaunchItem *item = [dataArray objectAtIndex:indexPath.row];
        [myTableViewDelegate tableViewItemDidSelected:item senderBtnTag:senderBtn.tag];
    }
    
    [self touchHelpBtnOnClick];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
